package com.zhongkun.datahome.web.rest;

import com.zhongkun.datahome.common.annotation.Log;
import com.zhongkun.datahome.common.core.domain.R;
import com.zhongkun.datahome.common.enums.BusinessType;
import com.zhongkun.datahome.service.req.cmd.appointment.AppointmentAddCmd;
import com.zhongkun.datahome.service.req.cmd.appointment.AppointmentUpdateCmd;
import com.zhongkun.datahome.service.req.cmd.appointment.AppointmentViewCmd;
import com.zhongkun.datahome.service.req.qry.appointment.AppointmentQry;
import com.zhongkun.datahome.service.res.PageVo;
import com.zhongkun.datahome.service.res.appointment.AppointmentVo;
import com.zhongkun.datahome.service.service.IAppointmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: MinKun
 * @Date: Created in 2025/6/26
 * @Description:
 */
@Api("小程序预约接口")
@RestController
@RequestMapping("/appointment")
@Slf4j
public class AppointmentController {

    @Resource
    private IAppointmentService appointmentService;

    @ApiOperation("分页查询预约列表")
    @GetMapping("/page")
    public R<PageVo<AppointmentVo>> page(AppointmentQry qry) {
        return R.ok(appointmentService.page(qry));
    }

    @ApiOperation("添加预约配置")
    @Log(title = "添加预约配置", businessType = BusinessType.INSERT)
    @PostMapping("addAppointment")
    public R<Boolean> addAppointment(@RequestBody AppointmentAddCmd cmd) {
        boolean check = appointmentService.checkCustom(cmd);
        if (!check) {
            return R.fail("客户时间段有重复，不允许设置");
        }
        boolean addSuccess = appointmentService.addAppointment(cmd);
        if (addSuccess) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @ApiOperation("编辑预约配置")
    @Log(title = "编辑预约配置", businessType = BusinessType.UPDATE)
    @PostMapping("updateAppointment")
    public R<Boolean> updateAppointment(@RequestBody AppointmentUpdateCmd cmd) {
        boolean check = appointmentService.checkUpdateCustom(cmd);
        if (!check) {
            return R.fail("客户时间段有重复，不允许设置");
        }
        boolean addSuccess = appointmentService.updateAppointment(cmd);
        if (addSuccess) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @ApiOperation("查询预约配置")
    @GetMapping("queryAppointmentView")
    public R<AppointmentViewCmd> queryAppointmentView(@Param("id") Long id, Long houseId) {
        return R.ok(appointmentService.queryAppointmentView(id, houseId));
    }

    @ApiOperation("删除预约配置")
    @DeleteMapping("/{id}")
    @Log(title = "删除预约配置", businessType = BusinessType.DELETE)
    public R<Void> delete(@PathVariable("id") Long id) {
        appointmentService.deleteAppointment(id);
        return R.ok();
    }

    @ApiOperation("导出预约配置")
    @Log(title = "导出预约配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(HttpServletResponse response, AppointmentQry qry) {
        appointmentService.export(qry, response);
    }
}
